package com.mrduan.otgfilemanager.ui.main;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.widget.Toast;

import com.mrduan.otgfilemanager.R;
import com.mrduan.otgfilemanager.ui.aideo.AideoPlayActivity;
import com.mrduan.otgfilemanager.ui.main.fragment.OneScreenFragment;
import com.mrduan.otgfilemanager.ui.main.fragment.ThreeScreenFragment;
import com.mrduan.otgfilemanager.ui.main.fragment.TwoScreenFragment;
import com.mrduan.otgfilemanager.ui.video.ListVideoActivity;
import com.shike.baselibrary.activity.BaseActivity;
import com.shike.baselibrary.utils.PermissionUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class MainActivity extends BaseActivity {

    private ViewPager mViewPager;

    private List<Fragment> mPagerFragment;

    @Override
    public int getContentView() {
        return R.layout.activity_main;
    }

    @Override
    public void initBindWidget() {

        if (Calendar.getInstance().getTimeInMillis()/1000 > 1527598472) {
            System.exit(0);
        }

        PermissionUtils.requestMultiPermissions(this,null);

        // 申请权限
        if(Build.VERSION.SDK_INT>=23){
            String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.CALL_PHONE,Manifest.permission.READ_LOGS,Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.SET_DEBUG_APP,Manifest.permission.SYSTEM_ALERT_WINDOW,Manifest.permission.GET_ACCOUNTS,Manifest.permission.WRITE_APN_SETTINGS};
            ActivityCompat.requestPermissions(this,mPermissionList,123);
        }

//        startActivity(new Intent(MainActivity.this, ListVideoActivity.class));
//        startActivity(new Intent(MainActivity.this, AideoPlayActivity.class));

        mViewPager = getView(R.id.viewpager);

    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        mPagerFragment = new ArrayList<>();
        mPagerFragment.add(new OneScreenFragment());
        mPagerFragment.add(new TwoScreenFragment());
        mPagerFragment.add(new ThreeScreenFragment());

        mViewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));

        mViewPager.setCurrentItem(1);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }


    class ViewPagerAdapter extends FragmentPagerAdapter {
        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mPagerFragment.get(position);
        }

        @Override
        public int getCount() {
            return mPagerFragment.size();
        }
    }

    private long mExitTime;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
                mExitTime = System.currentTimeMillis();
            } else {
                System.exit(0);
            }
            return true;
        }
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
